/* js for place page */

(function() {
    window.onload = function() {
      var placeMarkers = [];
      var map, selfMarker, options, center, lastZoomLevel;
      var homeLatitude, homeLongitude, homeLatLng;
      var lastLatitude, lastLongitude, lastLatLng;

      function autoFill(results) {
        $('#id_address').css({'color':'#1a1a1a'});
        document.getElementById('id_address').value = results[0].formatted_address;
      }

      homeLatitude = utils.readCookie('homeLatitude');
      homeLongitude = utils.readCookie('homeLongitude');
      homeLatLng = new google.maps.LatLng(homeLatitude, homeLongitude);

      lastLatitude = utils.readCookie('lastLatitude') || homeLatitude;
      lastLongitude = utils.readCookie('lastLongitude') || homeLongitude;
      lastLatLng = new google.maps.LatLng(lastLatitude, lastLongitude);

      center = lastLatLng || homeLatLng;
      lastZoomLevel = parseInt(utils.readCookie('lastZoomLevel'), 10) || 10;

      options = {
        center: center,
        zoom: lastZoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        navigationControl: true,
        navigationControlOptions: {
          position: google.maps.ControlPosition.RIGHT_BOTTOM,
          style: google.maps.NavigationControlStyle.Aj
        }
      };

      map = new google.maps.Map(document.getElementById('map-submit'), options);

      var control = document.createElement('div');
      //control.id = 'button-home';
      //control.style.position = 'absolute';
      //control.style.top = '10px';
      //control.style.right = '10px';
      control.innerHTML = '<img src="/static/img/map/home.png" width="50" height="50" />';
      //control.index = 1;

      // Add the control to the map
      map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);

      // When the button is clicked pan to sydney
      google.maps.event.addDomListener(control, 'click', function() {
          console.log('back to home');
          map.setCenter(homeLatLng);
          selfMarker.setPosition(homeLatLng);
          lastLatitude = homeLatitude;
          lastLongitude = homeLongitude;
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);    
          //map.panTo(homeLatLng);
      });

      google.maps.event.addListener(map, 'zoom_changed', function() {
          var zoomLevel = map.getZoom();
          utils.createCookie('lastZoomLevel', zoomLevel, 365);
          console.log('lastZoomLevel: ' + zoomLevel);
      });

      selfMarker = gmap.createSelfMarker(map, lastLatLng || homeLatLng);                             
      utils.writeLatLng(lastLatitude || homeLatitude, lastLongitude || homeLongitude);

      console.log('map has been created!');
      google.maps.event.addListener(selfMarker, 'dragend', function() {
          selfMarker.setOptions({icon: gmap.selfMarker});
          var lastLatLng = selfMarker.getPosition();
          var expires = new Date(Date.now() + gmap.ONE_DAY * 365).toUTCString();
          lastLatitude = lastLatLng.lat();
          lastLongitude = lastLatLng.lng();
          console.log('lastLatidue: ' + lastLatitude);
          console.log('lastLongitude: ' + lastLongitude);
          console.log('start set cookie');
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);
          console.log(lastLatitude);
          console.log(lastLongitude);
          console.log('end set cookie');
          map.setCenter(lastLatLng);

          utils.writeLatLng(lastLatitude, lastLongitude);

          // Call geocode
          gmap.geocode({'latLng':lastLatLng}, autoFill);
      });                                                                               

      $('textarea').val('说点什么').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});
      });

      $('input#id_name').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});
      });

      $('input#id_address').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});
      });

      $('input#id_tags').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});
      });                               

    };
})();
